package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.GrowthValRecord;
import com.zuipin.mapper.GrowthValRecordMapper;
import com.zuipin.service.IGrowthValRecordService;
import com.zuipin.service.IMemberAccountService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;

/**
 * 
 * @author zhuangjiajian
 * @date 2017-7-31 09:26:40
 */
@Service
public class GrowthValRecordService implements IGrowthValRecordService {
	
	@Resource
	private GrowthValRecordMapper	growthValRecordMapper;
	
	@Resource
	private IMemberAccountService	memberAccountService;
	
	@Override
	public GrowthValRecord findById(Long id) {
		// TODO Auto-generated method stub
		return growthValRecordMapper.findById(id);
	}
	
	@Override
	public List<GrowthValRecord> findList(Long memberId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void insert(GrowthValRecord growthValRecord) {
		// TODO Auto-generated method stub
		
		growthValRecord.setCreateTime(DateUtils.getCurrentDateTime());
		growthValRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		growthValRecord.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		growthValRecordMapper.insert(growthValRecord);
	}
	
	@Override
	public void update(GrowthValRecord growthValRecord) {
		// TODO Auto-generated method stub
		growthValRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		growthValRecordMapper.update(growthValRecord);
	}
	
	@Override
	public void deleteById(Long id) {
		GrowthValRecord growthValRecord = growthValRecordMapper.findById(id);
		growthValRecord.setDelFlag(ConstantsUtil.DEL_FLAG_YES_1);
		update(growthValRecord);
	}
	
	@Override
	public List<GrowthValRecord> findListByPage(GrowthValRecord growthValRecord, Pagination page) {
		// TODO Auto-generated method stub
		return growthValRecordMapper.findListByPage(growthValRecord, page);
	}
	
	@Override
	public Integer getCurrentGrowthVal(Long memberId, Long sysId) {
		// TODO Auto-generated method stub
		return growthValRecordMapper.getCurrentGrowthVal(memberId, sysId);
	}
	
	/*	@Transactional(rollbackFor = Exception.class)
		@Override
		public void addGrowthValRecord(Long memberId, Long sysId, Long createId, Long updateId, Integer alterGrowthVal, GrowthValType growthValType, String remark, String describe,
				String oddNumbers) throws CustomException {
			// TODO Auto-generated method stub
			// MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysId(memberId, sysId);
			MemberAccount memberAccount = memberAccountService.findByMemberIdAndSysIdForUpdate(memberId, sysId);
			if (memberAccount != null) {
				boolean isBinding = memberAccountService
						.isBindingMobilePhone(memberAccount.getMemberId());
				//账号未绑定手机号，不能操作
				if (!isBinding) {
					throw new CustomException(ErrorCode.DB_EXCEPTION.getCode(),
							"Account not binding mobile phone number, can't operation");
				}
				if (!(MemberLevel.AGENT_MEMBER_LEVEL_ID.equals(memberAccount.getMemberLevelId())) && alterGrowthVal != 0) {
					GrowthValRecord growthValRecord = new GrowthValRecord();
					growthValRecord.setMemberId(memberId);
					growthValRecord.setSysId(sysId);
					growthValRecord.setCreateId(createId);
					growthValRecord.setUpdateId(updateId);
					growthValRecord.setGrowthValType(growthValType);
					growthValRecord.setBeforeGrowthVal(memberAccount.getGrowthValue());
					growthValRecord.setAlterGrowthVal(alterGrowthVal);
					
					growthValRecord.setRemark(remark);
					growthValRecord.setDescribes(describe);
					growthValRecord.setOddNumbers(oddNumbers);
					// 修改后的金额
					int growthVal = memberAccount.getGrowthValue() == null ? 0 : memberAccount.getGrowthValue();
					
					// int cumulativeIntegral = memberAccount.getCumulativeIntegral() == null ? 0
					// : memberAccount.getCumulativeIntegral();
					// USE_SHOPPING购物积分抵扣 减少
					// 其余默认都是增加
					// if (IntegralType.USE_SHOPPING.equals(integralType)) {
					// totalIntegral = totalIntegral - alterPoint;
					// integralRecord.setAlterPoint(-alterPoint);
					// } else {//积分增加累计积分增加
					// totalIntegral = totalIntegral + alterPoint;
					// cumulativeIntegral = cumulativeIntegral + alterPoint;
					// integralRecord.setAlterPoint(alterPoint);
					// }
					growthVal = growthVal + alterGrowthVal;
					growthValRecord.setAfterGrowthVal(growthVal);
					
					// 可用成长值小于使用成长值，不能操作
					if (growthVal < 0 && alterGrowthVal < 0) {
						
						growthValRecord.setAfterGrowthVal(0);
						growthValRecord.setAlterGrowthVal(-1 * (memberAccount.getTotalIntegral() == null ? 0 : memberAccount.getTotalIntegral()));
						
					}
					
					insert(growthValRecord);// 新增记录
					// 更新账户
					MemberAccount tempMemberAccount = new MemberAccount();
					tempMemberAccount.setId(memberAccount.getId());
					tempMemberAccount.setTotalIntegral(growthVal);
					// tempMemberAccount.setCumulativeIntegral(cumulativeIntegral);
					// memberAccountService.update(tempMemberAccount);
					memberAccountService.updateIntegral(memberAccount.getId(), growthValRecord.getAlterGrowthVal());
					
				}
			} else {
				throw new CustomException(ErrorCode.DB_EXCEPTION.getCode(), "memberAccount is not exist");
			}
		}*/
	
}
